import os


def convert_to_utf8(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
    except UnicodeDecodeError:
        try:
            with open(file_path, 'r', encoding='gbk') as file:
                content = file.read()
        except Exception as e:
            print(f"无法读取文件 {file_path}: {e}")
            return

    try:
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(content)
        print(f"成功将 {file_path} 转换为 UTF-8 编码")
    except Exception as e:
        print(f"无法将 {file_path} 转换为 UTF-8 编码: {e}")


def main():
    current_dir = os.getcwd()
    for root, _, files in os.walk(current_dir):
        for file in files:
            if file.endswith(('.c', '.h', '.tex')):
                file_path = os.path.join(root, file)
                convert_to_utf8(file_path)


if __name__ == "__main__":
    main()
    